<?php

namespace app\admin\model;

use think\Model;
use traits\model\SoftDelete;
use addons\shopro\model\User;

class Recharge extends Model
{

    use SoftDelete;

    

    // 表名
    protected $table = 'recharge';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'status_text',
        'pay_type_text',
        'paytime_text',
        'xw_is_default_text',
        'xw_create_time_text'
    ];
    // 订单状态
    const STATUS_INVALID = -2;
    const STATUS_CANCEL = -1;
    const STATUS_NOPAY = 0;
    const STATUS_PAYED = 1;
    const STATUS_FINISH = 2;
    // 获取订单号
    public static function getSn($user_id)
    {
        $rand = $user_id < 9999 ? mt_rand(100000, 99999999) : mt_rand(100, 99999);
        $order_sn = 'cz'.date('Yhis') . $rand;

        $id = str_pad($user_id, (24 - strlen($order_sn)), '0', STR_PAD_BOTH);

        return $order_sn . $id;
    }
    public function addOne($type,$fee)
    {
        $user = User::info();
        $add = [
            'user_id'=>$user->id,
            'order_sn'=>self::getSn($user->id),
            'total_fee'=>$fee,
            'pay_type'=>$type,
        ];
        // $this->user_id=$user->id;
        // $this->order_sn=self::getSn($user->id);
        // $this->total_fee=$fee;
        // $this->pay_type=$type;
        return self::create($add);
        return $this->save();
    }
    public function getStatusList()
    {
        return ['-2' => __('Status -2'), '-1' => __('Status -1'), '0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2')];
    }

    public function getPayTypeList()
    {
        return ['wechat' => __('Pay_type wechat'), 'alipay' => __('Pay_type alipay')];
    }

    public function getXwIsDefaultList()
    {
        return ['1' => __('Xw_is_default 1'), '2' => __('Xw_is_default 2')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPayTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_type']) ? $data['pay_type'] : '');
        $list = $this->getPayTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPaytimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['paytime']) ? $data['paytime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getXwIsDefaultTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['xw_is_default']) ? $data['xw_is_default'] : '');
        $list = $this->getXwIsDefaultList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getXwCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['xw_create_time']) ? $data['xw_create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setPaytimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setXwCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


}
